package com.oaoa.main.action;

import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.oaoa.main.entity.TestEntity;
import com.oaoa.main.service.TestI;

@Controller
public class TestAciton {
	
	
	
	@Autowired
	TestI testi;

	@RequestMapping("/main")
	public String main() {
		return "/main/main";

	}
	@RequestMapping("/success")
	public String success() {
		return "/main/success";

	}
	/**
	 * 
	 * 描述:
	 * 
	 * @param testEntity
	 *            表单FORM,一个普通pojo即可，可提供字段自动转换赋值，@Valid 驗證注解
	 * @param bindingResult
	 *            存放 ”@ModelAttribute“验证模型字段错误信息
	 * @param modelmap
	 *            和request参数对应
	 * @return
	 * @author shanyong 2012-9-20 下午7:41:31
	 */
   
	@RequestMapping("main/doAdd")
	public String doTestAdd(
			@Valid @ModelAttribute("testEntity") TestEntity testEntity,
			BindingResult bindingResult, ModelMap modelmap) {

		if (bindingResult.hasErrors()) {
			return "main/main";

		}
		testi.addTest(testEntity);
		List<TestEntity> result = testi.getTestEntitys();
		modelmap.addAttribute("data", result);

		return "main/sucess";

	}

}
